#encoding:utf8

"""
 如果不使用UrlParserApi的话, 可以在这个文件中制定,
 特别是当需要登录, 或者需要生成cookie的时候
"""

import requests
import copy

from decorators.dec_todo import TODO

return_data = dict(code=0, msg="", data="")


def gen_cookie():
    raise NotImplementedError()


class Opener(object):
    def __init__(self, header=None):
        self.opener = None
        self.login(header=header)

    @TODO(status=0)
    def login(self, header=None):
        # TODO
        # you should update the cookie by browser debug information
        if header is None:
            if True:
                raise NotImplementedError("need to set headers")
            header = {
                "Host": "www.itjuzi.com",
                "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:49.0) Gecko/20100101 Firefox/49.0",
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Language": "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3",
                "Accept-Encoding": "gzip, deflate, br",
                "Cookie": "_ga=GA1.2.2057419966.1476352888; gr_user_id=ad27cc0d-66bd-42ad-9198-53b086c945f7; Hm_lvt_1c587ad486cdb6b962e94fc2002edf89=1479733227,1479734485,1479741599,1479741613; Hm_lpvt_1c587ad486cdb6b962e94fc2002edf89=1479746043; session=23114010507a7d96d768405584d71871782a74c3; identity=mavarick%40163.com; remember_code=0nHvvXk5Ub; gr_session_id_eee5a46c52000d401f969f4535bdaa78=4d0521d4-41a4-42de-8720-74f45e292d57; _gat=1",
                "Connection": "keep-alive"
            }

        session = requests.Session()
        if header:
            session.headers.update(header)
        self.opener = session
        # url = "http://www.itjuzi.com/company?scope=12&sub_scope=21&born_year=2016&sortby=foundtime&page=2"
        # s.get(url)

    def get_source(self, url, **kwargs):
        data = copy.deepcopy(return_data)
        resp = self.opener.get(url, timeout=60)
        content = resp.content
        data['data'] = content
        return data
